package com.lqk.demo.concurrencytools.threadpool.executors.scheduledthread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

import static com.lqk.demo.n2.util.Sleeper.sleep;

/**
 * @author lqk
 * @date 2022/1/10 15:19
 *
 * 正确处理线程池异常(所有种类的线程池异常)
 *  方法1：主动捉异常
 *  方法2：使用 Future
 */
@Slf4j
public class TestScheduledExecutorServiceException {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);

        pool.schedule(()->{
            log.debug("task1");
//            int i = 1/0;    // 不会有异常信息
            try {
                int i = 1/0;
            } catch (Exception e) {
                log.error("error : {}", e);
            }

            sleep(2);
        },1, TimeUnit.SECONDS);
        ExecutorService pool2 = Executors.newFixedThreadPool(1);
        Future<Boolean> task2 = pool2.submit(() -> {
            log.debug("task2");
            int i = 1 / 0;
            return true;
        });
        log.debug("task2 : {}", task2.get());
    }
}
